﻿using SomeKit.Serialization;

namespace SomeKit.Persistence
{
    public static partial class PersistenceHelper
    {
        /// <summary>
        /// Saves the <paramref name="obj"/> to the file <paramref name="path"/> using the SerializationHelper.WriteObjectAsJson serializer.
        /// </summary>
        /// <remarks>
        /// If the serializers provided for in SerializationHelper are used, ensure that all types are known (using the KnownType attribute).
        /// </remarks>
        /// <typeparam name="T">A type of object to save.</typeparam>
        /// <param name="obj">The object that should be saved.</param>
        /// <param name="path">The location where the object should be saved to.</param>
        public static void SaveAsJson<T>(this T obj, string path)
        {
            obj.Save(path, SerializationHelper.WriteObjectAsJson<T>);
        }

        /// <summary>
        /// Reads an object from the file <paramref name="path"/> using the SerializationHelper.ReadObjectFromJsonStream serializer.
        /// </summary>
        /// <remarks>
        /// If the serializers provided for in SerializationHelper are used, ensure that all types are known (using the KnownType attribute).
        /// </remarks>
        /// <typeparam name="T">A type of object to save.</typeparam>
        /// <param name="path">The location where the object should be read from.</param>
        /// <param name="objectReader"></param>
        /// <returns>The object read from the location <paramref name="path"/>.</returns>
        public static T LoadAsJson<T>(string path)
        {
            return Load(path, SerializationHelper.ReadObjectFromJsonStream<T>);
        }
    }
}
